#include "test.pb.h"
#include <string>
#include <iostream>

using namespace fixbug;

int main()
{
    GetFriendResponse rsp;
    ResultCode* rc = rsp.mutable_result();
    rc->set_errcode(0);

    // 添加一个好友到friendlist
    User* user = rsp.add_friendlist();
    user->set_name("张三丰");
    user->set_age(18); // 修正为设置uid，而不是age
    user->set_sex(User::MAN);

    User* user1 = rsp.add_friendlist();
    user1->set_name("张三丰");
    user1->set_age(18); // 修正为设置uid，而不是age
    user1->set_sex(User::woman);
    // 输出好友列表的大小
    std::cout << "Number of friends: " << rsp.friendlist_size() << std::endl;

    for(int i =0;i<rsp.friendlist_size();i++)
    {
        std::cout << "Friend name: " << rsp.friendlist(i).name()
                << ", Age: " << rsp.friendlist(i).age()
                << ", Sex: " << rsp.friendlist(i).sex()
                << std::endl;
    }

    //我想把上面的序列话一下
        
    std::string serialized_data;
    if (rsp.SerializeToString(&serialized_data)) {
        std::cout << "Serialization successful." << std::endl;
    } else {
        std::cerr << "Failed to serialize the object." << std::endl;
        return -1;
    }
    // std::cout << "Serialized data: " << serialized_data.size()<<serialized_data << std::endl;
    GetFriendResponse new_rsp;
    if (new_rsp.ParseFromString(serialized_data)) {
        std::cout << "Deserialization successful." << std::endl;

        // 打印反序列化后的数据
        std::cout << "Number of friends: " << new_rsp.friendlist_size() << std::endl;
        for (int i = 0; i < new_rsp.friendlist_size(); i++) {
            const User& u = new_rsp.friendlist(i);
            std::cout << "Friend name: " << u.name()
                      << ", Age: " << u.age()
                      << ", Sex: " << u.sex()
                      << std::endl;
        }
    } else {
        std::cerr << "Failed to parse the serialized data." << std::endl;
        return -1;
    }

    
    return 0;
}
